package com.example.ssmlab2.controller;

import com.example.ssmlab2.model.Book;

import com.example.ssmlab2.service.BookService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("book")
public class BookController extends BaseController{
    private final BookService bookService;

    public BookController(BookService bookService) {
        this.bookService = bookService;
    }

    @RequestMapping("removeById/{id}")
    private String removeById(@PathVariable("id")int id){

        bookService.remove(id);
        session.setAttribute("books",bookService.queryList("queryAll",null));
        return "redirect:/book.jsp";

    }
    @RequestMapping("queryById/{id}")
    private String queryById(@PathVariable("id")int id){

        session.setAttribute("book",bookService.queryById(id));
        return "redirect:/edit.jsp";
    }

    @RequestMapping("add")
    private String add(Book book){
        bookService.create(book);
        session.setAttribute("books",bookService.queryList("queryAll",null));
        return "redirect:/book.jsp";

    }
    @RequestMapping("queryAll")
    private  String queryAll(Book book){
        session.setAttribute("book",book);
        bookService.queryList("queryAll", null);
        session.setAttribute("books",bookService.queryList("queryAll",book));
        return "redirect:/book.jsp";
    }
}